﻿namespace Dyao.Book.Contract
{
    /// <summary>
    /// 字典收集
    /// </summary>
    public class DirtyCollection
    {
        private List<string> _item = new List<string>();
        /// <summary>
        /// 名称
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool this[string name]
        {
            get => Contains(name);
            set
            {
                if (value)
                    Add(name);
                else
                    Remove(name);
            }
        }
        /// <summary>
        /// 是否包含
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool Contains(string name)
        {
            return _item.Contains(name);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="name"></param>
        public void Add(string name)
        {
            if (!Contains(name))
            {
                _item.Add(name);
            }
        }
        /// <summary>
        /// 是否存在
        /// </summary>
        /// <returns></returns>
        public bool Any()
        {
            return _item.Count!=0;
        }
        /// <summary>
        /// 移除
        /// </summary>
        /// <param name="name"></param>
        public void Remove(string name)
        {
            _item.RemoveAll(a => a == name);
        }
        /// <summary>
        /// 清除
        /// </summary>
        public void Clear()
        {
            _item.Clear();
        }
        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public IReadOnlyList<string> GetAll()
        {
            return _item.AsReadOnly();
        }
    }
}
